from sympy import dsolve, latex, N
#from libs.antlr4_tex2sym.antlr4_tex2sym import tex2sym
import sys
sys.path.append("/home/sivan/0_0/projects/gitee/others")
from latex2sympy.process_latex import process_sympy

def deq(data):
    expr, unns = data['expr'], data['unns']
    result = {'latex': '', 'text': '', 'float': '', 'error': ''}
    if None == expr or '' == expr.strip():
        result['error'] = '请输入方程!'
        return result
    uLen = len(unns)
    if uLen > 1:
        exprs = transEqs(expr)
        if type(exprs) is str:
            result['error'] = exprs
            return result
    elif 1 == uLen:
        unns = unns[0]
        try: exprs = process_sympy(expr)
        except:
            result['error'] = '错误的方程格式: ' + str(expr)
            return result
    else:
        result['error'] = '未知数输入错误!'
        return result
    try: ansr = solve(exprs, unns)
    except:
        result['error'] = '解方程时出错: ' + str(exprs)
        return result
    result['latex'] = latex(ansr)
    result['text'] = str(ansr)
    rstFlt = ''
    precise = 6
    for i in ansr:
        if type(i) is tuple: rstFlt += str([N(j, precise) for j in i]) + " \n"
        else: rstFlt += str(N(ansr[i], precise)) + ", " if type(ansr) is dict else str(N(i, precise)) + ", "
    result['float'] = rstFlt[:-2]
    return result

if __name__ == '__main__':
    a = '- y(x) + f^{(2)} y(x) = e^{x}'
    from sympy import *
    f = Function('f')
    x = symbols('x')
    print(f)
    print(type(f))
    b = process_sympy(a)
    print(b)
    print(type(b))
    c = dsolve(b, f(x))
    print(latex(c))
